package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.saas.domain.entity.CommunicatePlanUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.subform.CommunicatePlanTeamEnum;
import com.xbongbong.paas.help.subform.abstracts.AbstractUserTeamTransferSubForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 访客计划es和数据库格式转化
 * @author 魏荣杰
 * @date 2020/8/24 09:34
 * @since
 * @version
 */
@Component
public class CommunicatePlanTeamTransferSubForm extends AbstractUserTeamTransferSubForm {
    private static final Logger LOG = LoggerFactory.getLogger(CommunicatePlanTeamTransferSubForm.class);

    @Override
    public PaasFormDataEntity transferTeamToFormData(UserTeamEntity userTeamEntity) {
        CommunicatePlanUserEntity communicatePlanUserEntity = (CommunicatePlanUserEntity) userTeamEntity;
        PaasFormDataEntity paasFormDataEntity = createPaasFormDataEntityForUserTeam(userTeamEntity);

        JSONObject teamJson = JSONObject.parseObject(JSON.toJSONString(communicatePlanUserEntity));

        JSONObject data = new JSONObject();

        CommunicatePlanTeamEnum[] communicatePlanTeamEnums = CommunicatePlanTeamEnum.values();
        for (CommunicatePlanTeamEnum communicatePlanTeamEnum : communicatePlanTeamEnums) {
            String dbAttr = communicatePlanTeamEnum.getDbAttr();
            String dataAttr = communicatePlanTeamEnum.getDataAttr();
            Integer fieldType = communicatePlanTeamEnum.getFieldType();
            formatDataForEs(teamJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    @Override
    public UserTeamEntity transferFormDataToTeam(PaasFormDataEntity paasFormDataEntity) {
        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        CommunicatePlanTeamEnum[] communicatePlanTeamEnums = CommunicatePlanTeamEnum.values();
        for (CommunicatePlanTeamEnum communicatePlanTeamEnum : communicatePlanTeamEnums) {
            String dbAttr = communicatePlanTeamEnum.getDbAttr();
            String dataAttr = communicatePlanTeamEnum.getDataAttr();
            Integer fieldType = communicatePlanTeamEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);

        }
        CommunicatePlanUserEntity communicatePlanUserEntity = JSON.parseObject(dbData.toJSONString(), CommunicatePlanUserEntity.class);
        communicatePlanUserEntity.setAddTime(paasFormDataEntity.getAddTime());
        communicatePlanUserEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        communicatePlanUserEntity.setCorpid(paasFormDataEntity.getCorpid());
        communicatePlanUserEntity.setDel(paasFormDataEntity.getDel());
        communicatePlanUserEntity.setId(paasFormDataEntity.getId());

        return communicatePlanUserEntity;
    }
}
